package cn.lesaas.esb.component.tuxedo;

import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.impl.UriEndpointComponent;

import bea.jolt.JoltSession;

public class TuxedoComponent extends UriEndpointComponent {
	private TuxedoConfiguration configuration;

	public TuxedoComponent() {
		super(TuxedoEndpoint.class);
	}

	public TuxedoComponent(Class<? extends Endpoint> endpointClass) {
		super(endpointClass);
	}

	public TuxedoComponent(CamelContext context) {
		super(context, TuxedoEndpoint.class);
	}

	@Override
	protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
		TuxedoConfiguration config;
		if (configuration != null) {
			config = configuration.copy();
		} else {
			config = new TuxedoConfiguration();
		}
		
		config = parseConfiguration(config, uri, parameters);
		
		TuxedoEndpoint endpoint = new TuxedoEndpoint(uri, this, config);
		setProperties(endpoint.getConfiguration(), parameters);
		return endpoint;
	}

	protected TuxedoConfiguration parseConfiguration(TuxedoConfiguration configuration, String uri,
			Map<String, Object> parameters) throws Exception {
		configuration.parseURI(new URI(uri), parameters, this);
		return configuration;
	}

	@Override
	protected void doStart() throws Exception {
		if (configuration == null) {
			configuration = new TuxedoConfiguration();
		}

		super.doStart();
	}
}
